package com.fz.service.project.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.project.projectPartMap.ProjectPartMapAdd;
import com.fz.dto.project.projectPartMap.ProjectPartMapQuery;
import com.fz.dto.project.projectPartMap.ProjectPartMapUpd;
import com.fz.entity.project.ProjectPartMap;
import com.fz.mapper.project.ProjectPartMapMapper;
import com.fz.service.project.ProjectPartMapService;
import com.fz.utils.JacksonUtils;
import com.fz.vo.project.projectPartMap.ProjectPartMapVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author :xmj
 * @date :2021/3/25 下午5:45
 */
@Service
@SuppressWarnings("unchecked")
public class ProjectPartMapServiceImpl implements ProjectPartMapService {

    @Resource
    private ProjectPartMapMapper projectPartMapMapper;

    /**
     * @param pageNum             pageNum
     * @param pageSize            pageSize
     * @param projectPartMapQuery projectPartMapQuery
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPartMap.ProjectPartMapVo>
     * @description 项目标段映射关系分页查询
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    public ResponseEntity<ProjectPartMapVo> findProjectPartMapList(Integer pageNum, Integer pageSize, ProjectPartMapQuery projectPartMapQuery) {
        Page<ProjectPartMap> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProjectPartMap> lambdaQueryWrapper = new LambdaQueryWrapper<ProjectPartMap>() {
            {
                //TODO 查询条件条件添加 在此拼接 根据业务需求
//                eq(projectPartMapQuery.getPartId() != null, ProjectPartMap::getPartId, projectPartMapQuery.getPartId());
            }
        };
        IPage<ProjectPartMapVo> projectPartMapList = projectPartMapMapper.findProjectPartMapList(page, lambdaQueryWrapper);
        return ResponseEntity.buildSuccess(projectPartMapList);
    }

    /**
     * @param projectPartMapAdd projectPartMapAdd
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPartMap.ProjectPartMapVo>
     * @description 项目标段映射关系新增
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ProjectPartMapVo> saveProjectPartMap(ProjectPartMapAdd projectPartMapAdd) {
        ProjectPartMap projectPartMap = JacksonUtils.obj2pojo(projectPartMapAdd, ProjectPartMap.class);
        projectPartMapMapper.insert(projectPartMap);
        return ResponseEntity.buildSuccess();
    }

    /**
     * @param projectPartMapUpd projectPartMapUpd
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPartMap.ProjectPartMapVo>
     * @description 项目标段映射关系修改
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ProjectPartMapVo> updateProjectPartMap(ProjectPartMapUpd projectPartMapUpd) {
        //需要重新查询 才会执行乐观锁操作 以下操作不会执行乐观锁
//        ProjectPartMap projectPartMap = JacksonUtils.obj2pojo(projectPartMapUpd, ProjectPartMap.class);
//        projectPartMapMapper.updateById(projectPartMap);
        //首先要查到数据才会进行乐观锁变动
        ProjectPartMap projectPartMap = projectPartMapMapper.selectById(projectPartMapUpd.getId());
        projectPartMap.setPartId(projectPartMapUpd.getPartId());
        projectPartMap.setPartNameOwn(projectPartMapUpd.getPartNameOwn());
        projectPartMap.setTenderNoOwn(projectPartMapUpd.getTenderNoOwn());
        projectPartMap.setTenderNoMap(projectPartMapUpd.getTenderNoMap());
        projectPartMap.setPublishChannel(projectPartMapUpd.getPublishChannel());
        projectPartMap.setPartNameMap(projectPartMapUpd.getPartNameMap());
        projectPartMap.setIsValid(projectPartMapUpd.getIsValid());
        projectPartMapMapper.updateById(projectPartMap);
        return ResponseEntity.buildSuccess();
    }

    /**
     * @param ids  ids
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPartMap.ProjectPartMapVo>
     * @description 项目标段映射关系删除
     * @params
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ProjectPartMapVo> delProjectPartMap(String ids) {
        Long[] longs = Convert.toLongArray(ids);
        projectPartMapMapper.deleteBatchIds(Arrays.asList(longs));
        return ResponseEntity.buildSuccess();
    }
}
